<?php


namespace app\admin\controller;



use app\admin\logic\GoodsLogic;
use app\admin\model\Brand;
use app\admin\model\Gcategory;
use app\admin\model\GoodsProp;
use think\facade\Request;
use think\facade\View;
use app\BaseController;
//商品设置
class Goods extends BaseController
{
    //商品列表
    public function goodsList()
    {
        //接值
        $param = Request::param();
        //拼接where语句
        $where= GoodsLogic::goodsWhere($param);
        //留值
        $data = GoodsLogic::goodsData($param);
        View::assign($data);
        View::assign('data',$data);
        //时间排序
        $order = GoodsLogic::getOrderTime($param);
        //查询语句
        $list = \app\admin\model\Goods::getGoodList($where,$data,$order);
        //关键字变色
        $list = GoodsLogic::goodsKeywords($list,$param);
        //分页
        $page = $list->render();
        View::assign(['list'=>$list,'page'=>$page]);
        //品牌查询
        $brand = Brand::select()->toArray();
        View::assign('brand',$brand);
        //分类查询
        $cate = Gcategory::getGoodsCate();
        View::assign('cate',$cate);
        return View::fetch();
    }

    //添加商品
    public function goodAdd()
    {
        //判断是是否为post方式提交
        if (Request::isPost()){
            //接值
            $post = Request::post();
            $add = GoodsLogic::addGoodsInfo($post);
            //上传单图
            list($strtic,$image) = Upload::getPathsInfo('image');
            if ($strtic){
                $add['images'] = $image;
            }
            //上传多图
            list($strtic,$images) = Upload::getPathsInfo('images');
            if ($strtic){
                $add['default_image'] = $images;
            }
            $res = \app\admin\model\Goods::addGoodsInfo($add);
            if ($res){

                $this->success('添加成功',url('goodsList'));
            }else{
                $this->error('添加失败');
            }
        }


        //下拉分类循环
        $cate_one = Gcategory::getGoodsCate();
        View::assign('cate_one',$cate_one);
        //下拉品牌循环
        $brand_one = Brand::field('brand_id,brand_name')->select()->toArray();
        View::assign('brand_one',$brand_one);
        return View::fetch();
    }
    //修改商品
    public function goodEdit()
    {
        if (Request::isPost()){
            $post = Request::post();
            $get = Request::get();
            //单图修改
            list($static,$image) = Upload::getPathsInfo('image');
            //单图上传处理
            if ($static && !empty($image)){
                list($path_dir,$path_date) = Upload::imgUpload($image);
                $edit['images'] = $path_dir;

            }
            if (!$static){
                $this->error($image);
            }
            //多图修改
            list($post,$new_img) = Upload::imgsUpload($post);
            $edit = GoodsLogic::editGoodsInfo($post);
            $obj = \app\admin\model\Goods::find($edit['goods_id']);
            $old = $obj->images;
            $old_images = $obj->default_image;
            $res = $obj->save($edit);
//            list($res,$msg) = \app\admin\model\Goods::editGoodsInfo($edit,[]);
            if ($res){
                //移动临时目录下的图片到正式目录
                if (!is_dir('./static/'.date('Y/m/d'))){
                    mkdir('./static/'.date('Y/m/d'),0777,true);
                }
                foreach ($new_img as $v){
                    copy('./static/'.$v['old'],'./static/'.$v['new']);
                     //删除临时目录下的文件
                    unlink('./static/'.$v['old']);
                }
                //删除   老图中的被删图片
                if (isset($post['old_img']) && !empty($old_images)){
                    //array_diff 参数1中存在的数据 参数2中不存在 会被取出来
                    $arr = array_diff($old_images,$post['old_img']);
                }else{
                    $arr = $old_images;
                }
                if (is_array($arr) && !empty($arr)){
                    foreach ($arr as $v){
                        if (!empty($v)){
                            unlink('./static/'.$v);
                        }
                    }
                }
                if ($static && !empty($image)){
                    //移动临时文件到正式目录下
                    $z_dir = './static/'.$path_date;
                    if (!is_dir($z_dir)){
                        mkdir($z_dir,0777,true);
                    }
                    copy('./static/'.$image,'./static/'.$path_dir);
                    unlink('./static/'.$image);
                }
                if ($static && !empty($old) && !empty($image)){
                    unlink('./static/'.$old);
                }


                if ($post['edit']==0){
                    $this->success('修改成功',url('',$get));
                }
                if ($post['edit']==1){
                    unset($get['id']);
                    $this->success('修改成功',url('goodsList',$get));
                }
            }else{
                $this->error('修改失败');
            }
        }else{
            $get = Request::get();
            $list = \app\admin\model\Goods::editGoodsInfo($get);
            View::assign('list',$list);
        }

        //下拉分类循环
        $cate_one = Gcategory::getGoodsCate();
        View::assign('cate_one',$cate_one);
        //下拉品牌循环
        $brand_one = Brand::field('brand_id,brand_name')->select()->toArray();
        View::assign('brand_one',$brand_one);
        return View::fetch();
    }
    //商品列表软删除
    public function goodDel()
    {
        //接值
        $get = Request::get();
        //模型层处理软删除
        list($res,$msg) = \app\admin\model\Goods::delGoodsInfo($get);
        if ($res){
            $this->success($msg,url('goodsList'));
        }else{
            $this->error($msg);
        }
    }
    //商品列表多删除
    public function goodDels()
    {
        //接值
        $get = Request::get();
        //模型层处理多删软删除
        $res = \app\admin\model\Goods::delsGoodsInfo($get);
        if ($res){
            $this->success('删除成功',url('goodsList'));
        }else{
            $this->error('删除失败');
        }
    }
    //商品列表回收站
    public function huishou()
    {
        //查询软删除的数据
        $list = \app\admin\model\Goods::huishouList();
        //分页
        $page = $list->render();
        View::assign(['list'=>$list,'page'=>$page]);
        return View::fetch();
    }
    //商品列表回收站恢复
    public function recovery()
    {
        //接值
        $get = Request::get();
        //模型层处理恢复
        list($res,$msg) = \app\admin\model\Goods::recoveryInfo($get);
        if ($res){
            $this->success('恢复成功',url('huishou'));
        }else{
            $this->error('恢复失败');
        }
    }
    //商品列表回收站真删除
    public function huishouDel()
    {
        //接值
        $get = Request::get();
        //模型层处理真删除
        list($res,$msg) = \app\admin\model\Goods::huishouDel($get);
        if ($res){
            $this->success('删除成功',url('huishou'));
        }else{
            $this->error('删除失败');
        }
    }

    //属性列表
    public function goodsPropList()
    {
        //接值
        $param = Request::param();
        //留值
        $data = GoodsLogic::propData($param);
        View::assign($data);
        //拼接where语句
        list($where,$whereOr) = GoodsLogic::propWhere($param);
        //查询语句
        $list = GoodsProp::getGoodsProp($where,$whereOr,$data);
        //分页
        $page = $list->render();
        //属性值查询
        $pvalue = GoodsProp::getPvalues();
        //属性值转中文
        $list = GoodsLogic::propValue($list,$pvalue);
        View::assign('pvalue',$pvalue);
        View::assign(['list'=>$list,'page'=>$page]);
        return View::fetch();
    }
    //属性列表添加
    public function propAdd()
    {
        if (Request::isPost()){
            //接值
            $post = Request::post();
            if (isset($post) && !empty($post['vid'])){
                //逻辑层处理添加数据
                $add = GoodsLogic::addPropInfo($post);
            }else if(isset($post) && empty($post['vid'])){
                $post['vid'] = [];
                //逻辑层处理添加数据
                $add = GoodsLogic::addPropInfo($post);
            }
            if (!empty($post['values']) && isset($post)){
                $adds = GoodsLogic::addPropsInfo($post);
            }else{
                $adds = [];
            }
            //模型层运行添加语句
            list($res,$msg) = GoodsProp::addPropAttr($add,$adds);
            //判断是是否添加成功
            if ($res){
                $this->success($msg,url('goodsPropList'));
            }else{
                $this->error($msg);
            }
        }
    }
    //属性列表修改
    public function goodPropEdit()
    {
        if (Request::isPost()){
            //接值
            $post = Request::post();
            if (isset($post) && !empty($post['vid'])){
                //逻辑层处理添加数据
                $edit = GoodsLogic::editPropInfo($post);
            }else if(isset($post) && empty($post['vid'])){
                $post['vid'] = [];
                //逻辑层处理添加数据
                $edit = GoodsLogic::addPropInfo($post);
            }
            if (!empty($post['values']) && isset($post)){
                $adds = GoodsLogic::addPropsInfo($post);
            }else{
                $adds = [];
            }
            //模型层运行添加语句
            list($res,$msg) = GoodsProp::editPropAttr($edit,$adds);
            //判断是是否添加成功
            if ($res){
                $this->success($msg,url('goodsPropList'));
            }else{
                $this->error($msg);
            }
        }else{
            //接值
            $get = Request::get();
            $line = GoodsProp::where('pid','=',$get['id'])->find()->toArray();
            $line['pvalues'] = explode(',',$line['pvalues']);
            View::assign('line',$line);
            //属性值查询
            $pvalue = GoodsProp::getPvalues();
            View::assign('pvalue',$pvalue);
        }
        return View::fetch();
    }
    //属性列表单个删除
    public function propDel()
    {
        //接值
        $get = Request::get();
        //模型层执行删除语句
        $del = GoodsProp::delProp($get);
        if ($del){
            $this->success('删除成功',url('goodsPropList'));
        }else{
            $this->error('删除失败');
        }
    }
    //属性列表多个删除
    public function propDels()
    {
        //接值
        $get = Request::get();
        //模型层执行删除语句
        $del = GoodsProp::delsProp($get);
        if ($del){
            $this->success('删除成功',url('goodsPropList'));
        }else{
            $this->error('删除失败');
        }
    }





}
